package com.colorful.insuranceproject.microDrug;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.TransactionRecordAdapter;
import com.colorful.insuranceproject.base.PLEActivity;
import com.colorful.insuranceproject.bean.TransactionRecordBean;
import com.colorful.insuranceproject.claims.ClaimsDetailsActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MicroDrugRequest;
import com.lovely3x.common.activities.refresh.CanLoadMore;
import com.lovely3x.common.activities.refresh.CanRefresh;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 交易记录
 */
public class TransactionRecordActivity extends PLEActivity implements AdapterView.OnItemClickListener, View.OnClickListener, CanLoadMore, CanRefresh {

    private static final int GET_TRANSACTION_RECORD_URL = 0X1;

    TransactionRecordAdapter transactionRecordAdapter;

    private MicroDrugRequest microDrugRequest;

    @Bind(R.id.lv_transaction_record)
    ListView lvTransactionRecord;

    @Bind(R.id.fl_fragment_list_empty_container)
    ViewGroup container;

    @Override
    protected int getContentView() {
        return R.layout.activity_transaction_record;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        setTitle(getString(R.string.transaction_record));
    }

    @Override
    protected void onViewInitialized() {
        microDrugRequest = new MicroDrugRequest(getHandler());
        transactionRecordAdapter = new TransactionRecordAdapter(null, mActivity);
        lvTransactionRecord.setAdapter(transactionRecordAdapter);
        lvTransactionRecord.setOnItemClickListener(this);
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        handleLoadingContent();
        getPageData();
    }

    private void getPageData() {
        if (microDrugRequest != null) {
            microDrugRequest.getTransactionRecordUrl(currentIndex, GET_TRANSACTION_RECORD_URL);
        }
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_TRANSACTION_RECORD_URL:
                handleRefreshState(response, transactionRecordAdapter);
                if (response.isSuccessful) {
                    onContentStatusChanged(LOADING_SUCCESSFUL);
                    if (currentIndex == 0 && ((List<TransactionRecordBean>) response.obj).size() == 0) {
                        onContentStatusChanged(LOADING_FAILURE, getString(R.string.not_find_data), this);
                    } else {
                        setPageData((List<TransactionRecordBean>) response.obj, response.addtional);
                    }
                } else {
                    if (response.addtional == DEFAULT_POST_FIRST_PAGE) {
                        onContentStatusChanged(LOADING_FAILURE, CodeTable.getInstance().getCodeDescription(response), this);
                    }
                    break;
                }
        }
    }

    private void setPageData(List<TransactionRecordBean> obj, int index) {
        switch (index) {
            case DEFAULT_POST_FIRST_PAGE:
                transactionRecordAdapter.setData(obj);
                break;
            default:
                transactionRecordAdapter.addDataToLast(obj);
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    public void onBeginRefresh() {
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        getPageData();
    }

    @Override
    public void onBeginLoadMore() {
        currentIndex++;
        getPageData();
    }

    @Override
    protected ViewGroup getEmptyContainerView() {
        return container;
    }

    @Override
    public void onClick(View v) {
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        handleLoadingContent();
        getPageData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        TransactionRecordBean bean = transactionRecordAdapter.getItem(position);
//        launchActivity(ClaimsDetailsActivity.class, ClaimsDetailsActivity.EXTRA_CLAIMS_ORDER_ID, bean.getInsuranceId());
    }
}
